package com.springdemo.websocket;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration  
@EnableWebMvc  
@EnableWebSocket 
public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {
	 	@Override  
	    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {  
	 		System.out.println("-------------------registerWebSocketHandlers----------------------------------");
	 		registry.addHandler(chatTestWebSocketHandler(),"/chatTestWebSocketHandler"); 
	 		registry.addHandler(systemWebSocketHandler(),"/chatTestWebSocketHandler/sockjs").setAllowedOrigins("*").withSockJS();  
	        registry.addHandler(systemWebSocketHandler(),"/webSocketServer");  
	        registry.addHandler(systemWebSocketHandler(),"/webSocketServer/sockjs").setAllowedOrigins("*").withSockJS();  
	         //registry.addHandler(systemWebSocketHandler(),"/webSocketServer").addInterceptors(new WebSocketHandshakeInterceptor());  
	         //registry.addHandler(systemWebSocketHandler(), "/sockjs/webSocketServer").addInterceptors(new WebSocketHandshakeInterceptor()).withSockJS();  
	         //registry.addHandler(systemWebSocketHandler(), "/webSocketServer/sockjs").withSockJS();  
	         /*registry.addHandler(systemWebSocketHandler(),"/ws").addInterceptors(new WebSocketHandshakeInterceptor()); 
	            registry.addHandler(systemWebSocketHandler(), "/ws/sockjs").addInterceptors(new WebSocketHandshakeInterceptor()) 
	                    .withSockJS();*/  
	    }  
	      
	    @Bean 
	    public WebSocketHandler systemWebSocketHandler(){  
	        return new SystemWebSocketHandler();  
	    }  
	    
	    @Bean  
	    public WebSocketHandler chatTestWebSocketHandler(){  
	        return new ChatTestWebSocketHandler();  
	    }
}
